package com.cn.goodsadmin.controller;

import com.cn.goodsadmin.po.Type;
import com.cn.goodsadmin.service.TypeService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


//@CrossOrigin(origins = {"http://10.10.10.143:8020"}, maxAge = 3600)
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class TypeController {

    @Autowired
    TypeService typeService;


    @LoginRequired
    @RequestMapping(value = "/getalltype", method = RequestMethod.GET,params = "appid")
    @ResponseBody
    public SupplyMap getallType(int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            List<Type> types=typeService.getAllType(appid);
            if(types!=null){
                supplyMap.put("types",types);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR,ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/addtype", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap addType(@RequestBody Type type) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            typeService.addType(type);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/updatetype", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateType(@RequestBody Type type) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            typeService.updateType(type);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/deletetype", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap deleteType(@RequestBody Type type) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            typeService.deleteType(type);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

}
